package com.unidt.www.module.expert.controller.admin;

import com.unidt.www.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.unidt.www.framework.common.pojo.CommonResult;
import com.unidt.www.framework.common.pojo.PageResult;
import com.unidt.www.framework.common.util.file.FileWithExcelUtil;
import com.unidt.www.framework.common.util.object.ConvertUtil;
import com.unidt.www.module.expert.domain.ExpertInfoDo;
import com.unidt.www.module.expert.form.ExpertInfoEditForm;
import com.unidt.www.module.expert.form.ExpertInfoQueryForm;
import com.unidt.www.module.expert.service.IExpertInfoService;
import com.unidt.www.module.expert.vo.*;
import com.unidt.www.module.system.dal.dataobject.organ.OrganCodeDo;
import com.unidt.www.module.system.service.organ.IOrganCodeService;
import com.unidt.www.module.system.service.organ.dto.OrganCodeExcelDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.unidt.www.framework.common.pojo.CommonResult.success;

/**
 * @Author sk
 * @Description: 专家库 数据读取
 * @Date 2022/11/16 13:21
 **/

@Api(tags = "管理后台-专家库")
@RestController
@RequestMapping("/expert/info")
@Validated
public class ExpertInfoController {

    @Autowired
    IExpertInfoService iExpertInfoService;

    @Autowired
    IOrganCodeService  iOrganCodeService;

    /**
     * 功能描述: 专家编辑
     * @author sk
     * @date 2023/1/3
     * @param expertInfoEditForm
     * @return com.unidt.www.framework.common.pojo.CommonResult
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑")
    public CommonResult edit(@RequestBody @Validated ExpertInfoEditForm expertInfoEditForm) {
        return CommonResult.from(iExpertInfoService.edit(expertInfoEditForm));
    }


    /**
     * 功能描述: 专家详情
     * @author sk
     * @date 2023/1/3
     * @param id
     * @return com.unidt.www.framework.common.pojo.CommonResult<com.unidt.www.module.expert.vo.ExpertInfoVo>
     */
    @PostMapping("/getDetail/{id}")
    @ApiOperation(value = "获取详情")
    public CommonResult<ExpertInfoVo> getDetail(@PathVariable(value = "id") Long id) {
        return CommonResult.success(iExpertInfoService.getDetail(id));
    }

    /**
     * 功能描述: 专家库分页查询
     * @author sk
     * @date 2023/1/3
     * @param queryForm
     * @return com.unidt.www.framework.common.pojo.CommonResult<com.unidt.www.framework.common.pojo.PageResult<com.unidt.www.module.expert.domain.ExpertInfoDo>>
     */
    @GetMapping("/queryPage")
    @ApiOperation(value = "专家库分页查询")
    public CommonResult<PageResult<ExpertInfoDo>> queryPage(
            ExpertInfoQueryForm queryForm) {
        return success(iExpertInfoService.queryPage(queryForm));
    }

    /**
     * 功能描述: 获取专家列表
     * @author sk
     * @date 2023/1/3
     * @param
     * @return com.unidt.www.framework.common.pojo.CommonResult<java.util.List<com.unidt.www.module.expert.domain.ExpertInfoDo>>
     */
    @GetMapping("/getList")
    @ApiOperation(value = "专家列表")
    public CommonResult<List<ExpertInfoDo>> getList() {
        return success(iExpertInfoService.getList());
    }

    /**
     * 功能描述: 删除专家
     * @author sk
     * @date 2023/1/3
     * @param id
     * @return com.unidt.www.framework.common.pojo.CommonResult
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public CommonResult delete(@PathVariable(value = "id") Long id) {
        return CommonResult.from(iExpertInfoService.delete(id));
    }

    /**
     * 功能描述: 批量删除
     * @author sk
     * @date 2023/1/3
     * @param ids
     * @return com.unidt.www.framework.common.pojo.CommonResult
     */
    @DeleteMapping("/deleteBatch")
    @ApiOperation(value = "批量删除")
    public CommonResult deleteBatch(@RequestBody List<Long> ids) {
        return CommonResult.from(iExpertInfoService.deleteBatch(ids));
    }

    /**
     * 功能描述:热门领域
     * @author sk
     * @date 2023/1/3
     * @param
     * @return com.unidt.www.framework.common.pojo.CommonResult<java.util.List<com.unidt.www.module.expert.vo.AreaExpertiseVo>>
     */

    @GetMapping("/getHotAreaExpertiseTag")
    @ApiOperation(value = "热门领域")
    public CommonResult<List<AreaExpertiseVo>> getHotAreaExpertiseTag() {
        return success(iExpertInfoService.getHotAreaExpertise());
    }

    /**
     * 功能描述: 获取专家标签
     * @author sk
     * @date 2023/1/3
     * @param
     * @return com.unidt.www.framework.common.pojo.CommonResult<java.util.List<java.lang.String>>
     */
    @GetMapping("/getTags")
    @ApiOperation(value = "标签列表")
    public CommonResult<List<String>> getTags() {
        return success(iExpertInfoService.getTags());
    }

    /**
     * 功能描述: 领域列表
     * @author sk
     * @date 2023/1/3
     * @param
     * @return com.unidt.www.framework.common.pojo.CommonResult<java.util.List<java.lang.String>>
     */
    @GetMapping("/getAreaExpertise")
    @ApiOperation(value = "领域列表")
    public CommonResult<List<String>> getAreaExpertise() {
        return success(iExpertInfoService.getAreaExpertise());
    }


    /**
     * 功能描述: 获取上传模板
     * @author sk
     * @date 2023/1/3
     * @param response
     * @return void
     */
    @GetMapping("/getExcelTemplate")
    @ApiOperation(value = "获取上传模板")
    public void getExcelTemplate(HttpServletResponse response) {

        List<OrganCodeDo> list =  iOrganCodeService.getAll(3);
        List<OrganCodeExcelDto> organCodeExcelDtos =
                ConvertUtil.convertList(list,OrganCodeExcelDto.class);

        FileWithExcelUtil.exportExcelWithSheet(new ArrayList<>(), "专家信息录入", ExpertInfoImportExcelVo.class, "行政区域编码对照表",organCodeExcelDtos,OrganCodeExcelDto.class,"专家信息录入模板.xlsx", response);

    }

    /**
     * 功能描述: 专家新信息导入
     * @author sk
     * @date 2023/1/3
     * @param file
     * @return com.unidt.www.framework.common.pojo.CommonResult<com.unidt.www.module.expert.vo.ExpertImportResult>
     */
    @PostMapping("/expertImport")
    @ApiOperation(value = "专家信息导入")
    public CommonResult<ExpertImportResult> expertImport(@RequestParam("file") MultipartFile file) {
        List<ExpertInfoExcelVo> excelFormList = FileWithExcelUtil.importExcel(file, 0, 1, ExpertInfoExcelVo.class);
        if(excelFormList.isEmpty()){
            return CommonResult.error(GlobalErrorCodeConstants.FAIL.getCode(),"未检索到有效数据");

        }
        return CommonResult.success(iExpertInfoService.importData(excelFormList));
    }

    /**
     * 功能描述: 批量导出
     * @author sk
     * @date 2023/1/3
     * @param queryForm
     * @param response
     * @return com.unidt.www.framework.common.pojo.CommonResult
     */
    @GetMapping("/downLoad")
    @ApiOperation(value = "批量导出")
    public CommonResult downLoad(
            ExpertInfoQueryForm queryForm,HttpServletResponse response) {
        List<ExpertInfoExcelVo> expertInfoDos = iExpertInfoService.queryAll(queryForm);
        FileWithExcelUtil.exportExcel(expertInfoDos,"专家列表", ExpertInfoExcelVo.class,"专家列表.xlsx",response);

        return CommonResult.success("下载成功");
    }



}
